package presentation.view;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FocusTraversalPolicy;
import java.awt.Point;
import java.util.Stack;

import javax.swing.JFrame;
import javax.swing.JPanel;
import util.Constantes;
import presentation.view.menu.MenuAjuda;

public class MainFrame extends JFrame {
    private static MainFrame instance;
    private JFrame detalhe;
    private Stack<JPanel> pilhaPainel = new Stack<JPanel>();
    public static FocusTraversalPolicy focusPolicy;

    private MainFrame() {
	super();
	initialize();
    }
    
    public void setTitulo( String title){
	this.setTitle(title);
    }

    public static MainFrame getInstance() {
	if(instance == null){
	    instance = new MainFrame();
	}
	return instance;
    }

    public void alterarPainel(JPanel panel, JPanel painelAnterior) {
	if (painelAnterior != null) {
	    pilhaPainel.push(painelAnterior);
	}
	MainFrame.getInstance().remove( MainFrame.getInstance().getContentPane());
	MainFrame.getInstance().setContentPane(panel);
	MainFrame.getInstance().requestFocus();
    }

    public void retiraPilhaPainel () {
	if (!pilhaPainel.empty()) {
	    pilhaPainel.pop();
	}
    }
    public JPanel getPainelAnterior() {
	if (pilhaPainel.size() > 0) {
	    return pilhaPainel.peek();
	} else {
	    return JanelaInicial.getInstance();
	}
    }

    public JFrame getDetalhe() {
	if(this.detalhe == null){
	    this.detalhe = new JFrame();
	}
	int X = MainFrame.getInstance().getX();
	X += MainFrame.getInstance().getWidth();
	int Y = MainFrame.getInstance().getY();
	Point ponto = new Point( X, Y);
	this.detalhe.setLocation(ponto);
	return this.detalhe;
    }

    public void setContentPane(Container contentPane){
	super.setContentPane(contentPane);
	this.setVisible(true);
	this.detalhe.setVisible(false);
    }

    private void initialize() {
	this.detalhe = new JFrame();
	this.setSize(Constantes.larguraTelas, Constantes.alturaTelas);
	this.setMinimumSize(new Dimension(Constantes.larguraTelas,  Constantes.alturaTelas));
	this.setMaximumSize(new Dimension(Constantes.larguraTelas,  Constantes.alturaTelas));
	this.setLocationRelativeTo(null);
	this.setTitle("Trabalho de POO");
	this.setResizable(false);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	this.setContentPane(new Login());
	this.setJMenuBar(new MenuAjuda ());

	setVisible(true);
    }

    public static void main( String args[])throws Exception {
	instance = new MainFrame();
    }
} 
